@include('admin.public.head')
@include('admin.public.laydate')

<div class="ibox-title">
    <h5>vip添加</h5>
</div>

<div class="ibox-content">
    <form class="form-horizontal">

        <div class="form-group">
            <label class="col-sm-3 control-label">vip：</label>
            <div class="col-sm-9">
                <input type="text" name="name" class="form-control" placeholder="vip名稱"
                       value="{{$info['vip']->name??''}}">
            </div>
        </div>
        <div class="hr-line-dashed"></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">有效天數：</label>
            <div class="col-sm-9">
                <input type="text" name="duration" class="form-control" placeholder="有效天數"
                       value="{{$info['vip']->duration??''}}">
            </div>
        </div>
        <div class="hr-line-dashed"></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">激活时间：</label>
            <div class="col-sm-9">
                <div class="recharge_agreement">
                    <input  name="activate" class="form-control" id="activate" type="text/plain" value="{{$info['vip']->activate??''}}" ></br>
                </div>
            </div>
        </div>
        <div class="hr-line-dashed"></div>

        <div class="form-group">
            <label class="col-sm-3 control-label">Class：</label>
            <div class="col-sm-9">
                <div style="padding-bottom: 15px">
                    <div class="col-sm-1 text-center pull-left">
                        <select class="form-control" name="store_id">
                            <option value="">門店列表</option>
                            @foreach($select_store as $k=>$v)
                                <option value="{{$k}}" @if($store_id == $k) selected="selected" @endif>{{$v}}</option>
                            @endforeach
                        </select>
                    </div>

                    @include('admin.public.class_base_info.where_classroom_teacher')

                    <div class="col-sm-1 text-center pull-left">
                        <input type="text" id="start_time" name="start_time" value="{{$start_time??''}}"
                               placeholder="开始时间" autocomplete="off" class="input form-control">
                    </div>

                    <div class="col-sm-1 text-center pull-left">
                        <input type="text" id="end_time" name="end_time" value="{{$end_time??''}}" placeholder="结束时间"
                               autocomplete="off" class="input form-control">
                    </div>

                    <div class="col-sm-1">
                        <select class="form-control" name="start_hour" id="startHour">
                            <option value="">时</option>
                            <option value="00">00</option>
                            <option value="01">01</option>
                            <option value="02">02</option>
                            <option value="03">03</option>
                            <option value="04">04</option>
                            <option value="05">05</option>
                            <option value="06">06</option>
                            <option value="07">07</option>
                            <option value="08">08</option>
                            <option value="09">09</option>
                            <option value="10">10</option>
                            <option value="11">11</option>
                            <option value="12">12</option>
                            <option value="13">13</option>
                            <option value="14">14</option>
                            <option value="15">15</option>
                            <option value="16">16</option>
                            <option value="17">17</option>
                            <option value="18">18</option>
                            <option value="19">19</option>
                            <option value="20">20</option>
                            <option value="21">21</option>
                            <option value="22">22</option>
                            <option value="23">23</option>
                        </select>
                    </div>

                    <div class="col-sm-1">
                        <select class="form-control" name="start_minute" id="startMinute">
                            <option value="">分</option>
                            <option value="00">00</option>
                            <option value="01">01</option>
                            <option value="02">02</option>
                            <option value="03">03</option>
                            <option value="04">04</option>
                            <option value="05">05</option>
                            <option value="06">06</option>
                            <option value="07">07</option>
                            <option value="08">08</option>
                            <option value="09">09</option>
                            <option value="10">10</option>
                            <option value="11">11</option>
                            <option value="12">12</option>
                            <option value="13">13</option>
                            <option value="14">14</option>
                            <option value="15">15</option>
                            <option value="16">16</option>
                            <option value="17">17</option>
                            <option value="18">18</option>
                            <option value="19">19</option>
                            <option value="20">20</option>
                            <option value="21">21</option>
                            <option value="22">22</option>
                            <option value="23">23</option>
                            <option value="24">24</option>
                            <option value="25">25</option>
                            <option value="26">26</option>
                            <option value="27">27</option>
                            <option value="28">28</option>
                            <option value="29">29</option>
                            <option value="30">30</option>
                            <option value="31">31</option>
                            <option value="32">32</option>
                            <option value="33">33</option>
                            <option value="34">34</option>
                            <option value="35">35</option>
                            <option value="36">36</option>
                            <option value="37">37</option>
                            <option value="38">38</option>
                            <option value="39">39</option>
                            <option value="40">40</option>
                            <option value="41">41</option>
                            <option value="42">42</option>
                            <option value="43">43</option>
                            <option value="44">44</option>
                            <option value="45">45</option>
                            <option value="46">46</option>
                            <option value="47">47</option>
                            <option value="48">48</option>
                            <option value="49">49</option>
                            <option value="50">50</option>
                            <option value="51">51</option>
                            <option value="52">52</option>
                            <option value="53">53</option>
                            <option value="54">54</option>
                            <option value="55">55</option>
                            <option value="56">56</option>
                            <option value="57">57</option>
                            <option value="58">58</option>
                            <option value="59">59</option>
                        </select>
                    </div>
                    <div class="col-sm-1 text-center pull-left"><a href="javascript:void(0)" class="btn btn-primary"
                                                                   id="search">搜索</a></div>

                    <div class="col-sm-0 text-center pull-right" style="margin-bottom: 15px">
                        <input type="checkbox" name="" class="input form-control"
                               style="height: 20px;width: 20px;margin-right: 35px;margin-bottom: 20px" id="quanju">
                    </div>

                    @include('admin.public.laydate')

                </div>
                <style>
                    .responsive_input {
                        width: 100%;
                        height: 400px;
                        overflow: scroll;
                        overflow-x: hidden;
                    }

                    .class_base p {
                        float: left;
                        font-weight: bold;
                    }

                    .class_base p span {
                        color: red;
                    }

                    .class_base::-webkit-scrollbar {
                        display: none;
                    }

                    .span_c {
                        padding-right: 20px;
                    }
                </style>
                <div class="table-responsive responsive_input">
                    <table class="table">
                        <tbody style="position:relative;" id="class_tr">
                        @foreach($class_base_info as $v)
                            <tr>
                                <td class="class_base">
                                    <p>
                                        <span>課室</span>：<em class="span_c">{{$v->classsroom->numbering??''}}</em><span>教師</span>：<em
                                            class="span_c">
                                            @foreach($v->class_base_info_teacher as $vv)
                                                @if(!empty($vv->teacher))
                                                    {{$vv->teacher->teacher_name??''}}&nbsp;&nbsp;
                                                @endif
                                            @endforeach</em><span>課程</span>：<em
                                            class="span_c">{{$v->class_base->class_name??''}}</em><span>周期</span>：<em
                                            class="span_c">{{$v->class_week??''}}</em><span>時段</span>：<em
                                            class="span_c">{{$v->class_time??''}}</em><span>季節</span>：<em
                                            class="span_c">{{$v->quarterly->name??''}}</em>
                                    </p>
                                </td>
                                <td>
                                    @php
                                        $result_num = $info['num'][$v->id]??"";
                                        $class_info_id = $info['class_info_id']??[];

                                        $num_value = "";

                                        if(!empty($result_num))$num_value = $result_num;
                                    @endphp
                                    <input type="text" name="select_num" @if(in_array($v->id, $class_info_id)) disabled
                                           @endif class="form-control col-sm-1"
                                           placeholder="0或空則無限制" value="{{$num_value??""}}">
                                </td>
                                <td>
                                    <input type="checkbox"
                                           @if(in_array($v->id, $class_info_id)) checked @endif id="{{$v->id}}"
                                           name="class_base_info" class="i-checks"
                                           style="width: 20px;height: 20px;float: right;margin-right: 20px"
                                           value="{{$v->id}}">
                                </td>
                            </tr>
                        @endforeach;
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="hr-line-dashed" style="clear: both"></div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-9">
                    <a style="float:right;padding-right: 35px" onclick="delect_select_all()">全部删除</a>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">SelectClass：</label>
                <div class="col-sm-9">
                    <div class="table-responsive responsive_input">
                        <table class="table" id="tr">
                            @if(!empty($id))
                                @foreach($info['class_edit_info'] as $v)
                                    @php
                                        $result_num = $info['num'][$v->id]??"";

                                        $num_value = "";

                                        if(!empty($result_num))$num_value = $result_num;
                                    @endphp
                                    <tr class="select_class_{{$v->id}} class_base">
                                        <td>
                                            <input type="hidden" name="class_base_info_id[]"
                                                   class="form-control col-sm-1" placeholder="" value="{{$v->id}}">
                                            <input type="hidden" name="sum[]" class="form-control col-sm-1"
                                                   placeholder="0或空則無限制" value="{{$num[$v->id]??0}}">
                                            <p>
                                                <span>課室</span>：<em
                                                    class="span_c">{{$v->classsroom->numbering??''}}</em><span>教師</span>：<em
                                                    class="span_c">
                                                    @foreach($v->class_base_info_teacher as $vv)
                                                        @if(!empty($vv->teacher))
                                                            {{$vv->teacher->teacher_name??''}}&nbsp;&nbsp;
                                                        @endif
                                                    @endforeach
                                                </em><span>課程</span>：<em
                                                    class="span_c">{{$v->class_base->class_name??''}}</em><span>周期</span>：<em
                                                    class="span_c">{{$v->class_week??''}}</em><span>時段</span>：<em
                                                    class="span_c">{{$v->class_time??''}}</em><span>季節</span>：<em
                                                    class="span_c">{{$v->quarterly->name??''}}</em>
                                            </p>
                                            <p>
                                                <span>可預約數</span>：<em class="span_c">@if(empty($num_value))
                                                        無限制 @else {{$num_value}} @endif</em></p><a style="float:right"
                                                                                                   onclick="delect_select_class($(this))">删除</a>
                                        </td>
                                    </tr>
                                @endforeach
                            @endif
                        </table>
                    </div>
                </div>
            </div>
            {{--        <div class="hr-line-dashed"></div>--}}

            <input type="hidden" name="id" value="{{$id??0}}">
            <div class="form-group">
                <div class="col-sm-4 col-sm-offset-3">
                    <button class="btn btn-primary do" type="submit">保存</button>
                    <a href="/vip" class="btn btn-primary">返回vip</a>
                </div>
            </div>
    </form>
</div>

<script>

    laydate.render({
        elem: '#activate',
        max: '2099-06-16 23:59:59', //最大日期
        istime: true,
        istoday: true,
        choose: function (datas) {
            end.min = datas; //开始日选好后，重置结束日的最小日期
            end.start = datas //将结束日的初始值设定为开始日
        }
    });


    $("#search").click(function () {
        var form = new FormData();

        let store_id = $("select[name='store_id']").find("option:selected").val();
        let teacher = $("select[name='teacher']").find("option:selected").val();
        let classroom = $("select[name='classroom']").find("option:selected").val();
        let start_hour = $("select[name='start_hour']").find("option:selected").val();
        let start_minute = $("select[name='start_minute']").find("option:selected").val();
        let start_time = $("input[name='start_time']").val();
        let end_time = $("input[name='end_time']").val();

        form.append("store_id", store_id);
        form.append("teacher", teacher);
        form.append("classroom", classroom);
        form.append("start_time", start_time);
        form.append("end_time", end_time);
        form.append("start_hour", start_hour);
        form.append("start_minute", start_minute);

        var open = layer.load(2);

        $.ajax({
            type: 'POST',
            url: '/vip_search',
            dataType: "json",   //返回格式为json
            data: form,
            processData: false,
            contentType: false,
            success: function (result) {

                layer.close(open);

                let select_result = "";

                let class_result = result.data;

                for (var i = 0; i < class_result.length; i++) {

                    var numbering = '', teacher_name = '', class_name = '', class_week = '', class_time_string = '',
                        quarterly_name = '', id = '', select_id = '', class_base_info_teacher = '';

                    if (class_result[i].classsroom != null) {
                        numbering = class_result[i].classsroom.numbering;
                    }

                    if (class_result[i].teacher != null) {
                        teacher_name = class_result[i].teacher.teacher_name;
                    }

                    if (class_result[i].class_base != null) {
                        class_name = class_result[i].class_base.class_name;
                    }

                    if (typeof (class_result[i].class_week) != 'undefined') {
                        class_week = class_result[i].class_week;
                    }

                    if (typeof (class_result[i].class_time) != 'undefined') {
                        var class_time_start = class_result[i].class_time.time_start;
                        var class_time_end = class_result[i].class_time.time_end;

                        class_time_string = class_time_start + "~" + class_time_end;
                    }

                    if (typeof (class_result[i].class_base_info_teacher) != 'undefined') {
                        class_base_info_teacher = class_result[i].class_base_info_teacher;
                    }

                    if (class_result[i].quarterly != null) {
                        quarterly_name = class_result[i].quarterly.name;
                    }

                    if (typeof (class_result[i].id) != 'undefined') {
                        id = class_result[i].id;
                    }

                    select_result += '<tr>';
                    select_result += '<td class="class_base">';
                    select_result += '<p>';
                    select_result += '<span>課室</span>：<em class="span_c">' + numbering + '</em>';
                    select_result += '<span>教師</span>：<em class="span_c">';


                    if (typeof (class_base_info_teacher) != "undefined") {
                        if (class_base_info_teacher.length != 0) {
                            for (var t = 0; t < class_base_info_teacher.length; t++) {
                                if (class_base_info_teacher[t].teacher != null) {
                                    select_result += class_base_info_teacher[t].teacher.teacher_name + "&nbsp;&nbsp;";
                                }
                            }
                        } else {
                            select_result += '';
                        }
                    }

                    select_result += '</em>';
                    select_result += '<span>課程</span>：<em class="span_c">' + class_name + '</em>';
                    select_result += '<span>周期</span>：<em class="span_c">' + class_week + '</em>';
                    select_result += '<span>時段</span>：<em class="span_c">' + class_time_string + '</em>';
                    select_result += '<span>季節</span>：<em class="span_c">' + quarterly_name + '</em>';
                    select_result += '</p></td><td><input type="text" ';

                    select_id = $(".select_class_" + id).find("input[name='class_base_info_id[]']").val();
                    select_sum = $(".select_class_" + id).find("input[name='sum[]']").val();

                    if (select_id == id) {
                        select_result += 'disabled';
                    }

                    if (typeof (select_sum) == "undefined" || select_sum == 0) {
                        select_result += ' name="select_num" class="form-control col-sm-1"placeholder="0或空則無限制" value=""></td><td>';
                    } else {
                        select_result += ' name="select_num" class="form-control col-sm-1"placeholder="0或空則無限制" value="' + select_sum + '"></td><td>';
                    }

                    select_result += '<input type="checkbox"';

                    if (select_id == id) {
                        select_result += 'checked';
                    }

                    select_result += ' id="' + id + '" name="class_base_info" class="i-checks" style="width: 20px;height: 20px;float: right;margin-right: 20px" value="' + id + '">';
                    select_result += '</td></tr>';
                }

                $("#class_tr").empty();
                $("#class_tr").html(select_result).change();
            }
        });
    })

    function delect_select_all() {
        $("input[name='class_base_info']").prop("checked", false).change();
        $("#quanju").prop("checked", false).change();
        $("#tr").empty();
    }

    function delect_select_class(obj) {
        id = obj.parents("tr").find("input[name='class_base_info_id[]']").val();
        obj.parents("tr").remove();
        $("#" + id).prop("checked", false).change();
    }

    $("#quanju").change(function () {
        if ($(this).is(":checked")) {
            $("input[name='class_base_info']").prop("checked", true).change();
        } else {
            $("input[name='class_base_info']").prop("checked", false).change();
        }
    })


    $("#class_tr").delegate("input[name=class_base_info]:checkbox", "change", function () {

        obj = $(this);

        select_num_obj = obj.parents('tr').find("input[name='select_num']");

        let class_base_info_id = obj.val();
        let select_num = select_num_obj.val();
        if (select_num == '') select_num = 0;
        let text = obj.parents("tr").find("td").eq(0).html();

        if (obj.is(":checked")) {

            select_num_obj.prop('disabled', true);

            //选中
            let select_html = "";

            //防止重复添加
            $("#tr .select_class_" + class_base_info_id).remove();

            select_html += '<tr class="select_class_' + class_base_info_id + ' class_base"><td><input type="hidden" name="class_base_info_id[]" class="form-control col-sm-1" placeholder="" value="' + class_base_info_id + '" ><input type="hidden" name="sum[]" class="form-control col-sm-1" placeholder="0或空則無限制" value="' + select_num + '" >' + text + '<p><span>可預約數</span>：<em class="span_c">';

            if (select_num == 0) {
                select_html += '無限制';
            } else {
                select_html += select_num;
            }

            select_html += "</em></p>";

            select_html += '<a style="float:right" onclick="delect_select_class($(this))">删除</a>';

            select_html += '</td></tr>';

            $("#tr").append(select_html);

        } else {

            select_num_obj.prop('disabled', false);

            //取消
            $("#tr").find(".select_class_" + class_base_info_id).remove();

        }


    })

    // function selection(obj)
    // {
    //
    // }

    $('.do').click(function () {
        var form = new FormData();

        var class_base_info_id = new Array();
        var num = new Array();

        $('input[name="class_base_info_id[]"]').each(function () {
            class_base_info_id.push($(this).val());
        });

        $('input[name="sum[]"]').each(function () {
            num.push($(this).val());
        });

        var name = $('input[name="name"]').val();

        var duration = $('input[name="duration"]').val();

        var id = $('input[name="id"]').val();

        let activate = $("input[name='activate']").val();

        form.append("num", num);
        form.append("class_base_info_id", class_base_info_id);
        form.append("name", name);
        form.append("id", id);
        form.append("duration", duration);
        form.append("activate", activate);

        $.ajax({
            type: 'POST',
            url: '/vip_do',
            dataType: "json",   //返回格式为json
            data: form,
            processData: false,
            contentType: false,
            success: function (data) {

                if (data.status == '1') {
                    var swal_json = {
                        title: data.message,
                        type: "success"
                    }

                    swal(swal_json, function (isConfirm) {
                        window.location.reload();
                    });

                } else {
                    var swal_json = {
                        title: data.message,
                        type: "error"
                    }

                    swal(swal_json, function (isConfirm) {

                    });
                }
            }
        });

        return false;
    });
</script>
